package com.cj.framework.util;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.cj.framework.app.BaseApplication;
import com.cj.framework.config.Config;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.Map;

/**
 * Created by cj on 2016/6/7.
 * Email:781806481@qq.com
 */
public class HttpImp implements IHttp {
    private static final String TAG="HttpImp";
    private RequestQueue requestQueue;
    public HttpImp(){
        requestQueue= Volley.newRequestQueue(BaseApplication.getContext());
    }

    /**
     * 获取字符串（get方法）
     * @param url
     * @param callback
     */
    @Override
    public void httpInfoGet(String url, final HttpInfoCallback callback) {
        StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callback.onSucceed(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.onFailed(error);
            }
        });
        requestQueue.add(stringRequest);
    }

    @Override
    public void httpInfoGetWithIndex(String url, final HttpInfoCallbackWithIndex callback, final int index) {
        StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callback.onSucceed(response,index);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.onFailed(error,index);
            }
        });
        requestQueue.add(stringRequest);
    }

    /**
     * 获取字符串（post方法）
     * @param url
     * @param map
     * @param callback
     */
    @Override
    public void httpInfoPost(String url, final Map<String, String> map, final HttpInfoCallback callback) {
        final StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callback.onSucceed(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.onFailed(error);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    @Override
    public void httpInfoPostWithIndex(String url, final Map<String, String> map, final HttpInfoCallbackWithIndex callbackWithIndex, final int index) {
        final StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callbackWithIndex.onSucceed(response,index);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callbackWithIndex.onFailed(error,index);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    /**
     * 获取图片
     * @param url
     * @param callback
     */
    @Override
    public void httpBitmapGet(String url, final HttpInfoCallback callback) {
        ImageLoader.getInstance().loadImage(url, Config.getOptions(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                Log.d(TAG,"onLoadingStarted");
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                Log.d(TAG,"onLoadingFailed"+failReason.toString());
                callback.onFailed(new Exception());
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                callback.onSucceed(loadedImage);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                Log.d(TAG,"onLoadingCancelled");
            }
        });
    }

    @Override
    public void httpBitmapGetWithIndex(String url, final HttpInfoCallbackWithIndex callback, final int index) {
        ImageLoader.getInstance().loadImage(url, Config.getOptions(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                Log.d(TAG,"onLoadingStarted");
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                Log.d(TAG,"onLoadingFailed"+failReason.toString());
                callback.onFailed(new Exception(),index);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                callback.onSucceed(loadedImage,index);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                Log.d(TAG,"onLoadingCancelled");
            }
        });
    }

    /**
     * 获取图片（指定大小）
     * @param url
     * @param callback
     * @param width
     * @param height
     */
    @Override
    public void httpBitmapGet(String url, final HttpInfoCallback callback, int width, int height) {
        ImageSize size=new ImageSize(width,height);
        ImageLoader.getInstance().loadImage(url, size,Config.getOptions(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                Log.d(TAG,"onLoadingFailed"+failReason.toString());
                callback.onFailed(new Exception());
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                callback.onSucceed(loadedImage);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                Log.d(TAG,"onLoadingCancelled");
            }
        });
    }

    @Override
    public void httpBitmapGet(String url, ImageView imageView) {
        ImageLoader.getInstance().displayImage(url,imageView,Config.getOptions());
    }


}
